// utils/storage.js

// localStorage 封装
export const storage = {
  set: (key, value) => {
    try {
      const serializedValue = JSON.stringify(value)
      localStorage.setItem(key, serializedValue)
    } catch (error) {
      console.error('存储失败:', error)
    }
  },
  
  get: (key, defaultValue = null) => {
    try {
      const item = localStorage.getItem(key)
      return item ? JSON.parse(item) : defaultValue
    } catch (error) {
      console.error('读取存储失败:', error)
      return defaultValue
    }
  },
  
  remove: (key) => {
    try {
      localStorage.removeItem(key)
    } catch (error) {
      console.error('删除存储失败:', error)
    }
  },
  
  clear: () => {
    try {
      localStorage.clear()
    } catch (error) {
      console.error('清空存储失败:', error)
    }
  }
}

// token 相关操作
export const TOKEN_KEY = 'auth_token'
export const USER_INFO_KEY = 'user_info'

export const setToken = (token) => storage.set(TOKEN_KEY, token)
export const getToken = () => storage.get(TOKEN_KEY)
export const removeToken = () => storage.remove(TOKEN_KEY)

export const setUserInfo = (userInfo) => storage.set(USER_INFO_KEY, userInfo)
export const getUserInfo = () => storage.get(USER_INFO_KEY)
export const removeUserInfo = () => storage.remove(USER_INFO_KEY)